<?php
namespace app\modules\admin\controllers;

use app\constants\Code;
use app\extensions\XUtils;
use yii;
use yii\data\Pagination;
use app\libs\XHttp;
/**
 * 工具控制器
 * @author chenym
 * @since 2017.9.28
 * Class ToolController
 */
class ToolController extends BaseController
{
	public $servSalt = "bwgame" ;
	public $serverList = [ "uvtong" => "铜埔测试","chenym" => "勇明测试","zhaoyc" => "永灿测试","publicOne" => "公共测试1服", "publicTwo" => "公共测试2服", "extraNet" => "外网服"];
	public $prop = [
	    "gold" => "金币", // "gold",      --金币
	    "diamond" => "钻石", // "diamond",   --钻石
	    "stone_ore" => "石矿", // "stone_ore", --石矿
	    "iron_ore" => "铁矿", // "iron_ore",  --铁矿
	    "gold_ore" => "金矿", // "gold_ore",  --金矿
	    "crys_ore" => "晶矿", // "crys_ore",  --晶矿
	    "jade_ore" => "玉矿", // "jade_ore",  --玉矿
    ];

    public function init()
    {
        parent::init();
    }
	/**
	 * GM工具
	 * 
	 */
	public function actionGm()
	{
		$request = Yii::$app->request;
		
		if ($request->isPost){
			$result = ["status" => false,"msg" => "请求失败"];
			$serverId = $request->post('serverId');
			$roleId = $request->post('roleId');
			$propId   = $request->post('propId');
			$count    = (int)$request->post('count');
			if(!$this->serverList[$serverId]) {
				$result["msg"] = "服务器不存在";
				exit(json_encode($result));
			}
			if(!$this->prop[$propId]) {
				$result["msg"] = "道具不存在";
				exit(json_encode($result));
			}
			
			$data["time"] = time();
			$data["sign"] = md5($data["time"] . Yii::$app->params["md5Salt"]["gm"]);

			$url = Yii::$app->params["apiHost"][$serverId] . "/gm";
			$data["obj"] = "user";
			$data["method"] = "gm";
			$data["path"] = "gm";
			$data["role_id"] = $roleId;
			$data["cmd"] = '$addcoin ' . $propId . ' ' . $count;
	
			//http://127.0.0.1:7006/gm?obj=user&method=gm&path=gm&role_id=4628f207ee0e0001&cmd='$addcoin gold 100'
			$res = XHttp::get($url, $data);

			if($res) {
				$result["status"] = true;
				$result["msg"] = "请求成功";
			} 
			exit(json_encode($result));
		}
		$assign = ["serverList" => $this->serverList , "propList" => $this->prop];
		return $this->render('gm', $assign);
	}

}